<template>
  <div class="personal-info-container">
    <el-form :model="userInfo" label-width="100px" class="centered-form styled-form">
      <!-- 移除头像的 el-form-item 标签中的 label 属性，删除“头像”二字 -->
      <el-form-item>
        <el-upload
          class="avatar-uploader circular-avatar"
          action="#"
          :show-file-list="false"
          :on-success="handleAvatarSuccess"
          :before-upload="beforeAvatarUpload"
        >
          <!-- 头像显示为圆形 -->
          <img v-if="userInfo.avatar" :src="userInfo.avatar" class="avatar circular">
          <i v-else class="el-icon-plus avatar-uploader-icon"></i>
        </el-upload>
      </el-form-item>
      <el-form-item label="用户名">
        <el-input v-model="userInfo.username" class="small-input"></el-input>
      </el-form-item>
      <el-form-item label="年龄">
        <el-input-number v-model="userInfo.age" :min="0" :max="120" class="small-input"></el-input-number>
      </el-form-item>
      <el-form-item label="性别">
        <el-radio-group v-model="userInfo.gender" class="small-input">
          <el-radio label="男"></el-radio>
          <el-radio label="女"></el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="手机号">
        <el-input v-model="userInfo.phone" class="small-input"></el-input>
      </el-form-item>
      <el-form-item label="邮箱">
        <el-input v-model="userInfo.email" class="small-input"></el-input>
      </el-form-item>
      <el-form-item label="身份">
        <el-select v-model="userInfo.role" placeholder="请选择身份" class="small-input">
          <el-option label="管理员" value="管理员"></el-option>
          <el-option label="普通用户" value="普通用户"></el-option>
        </el-select>
      </el-form-item>
      <!-- 新增密码输入项 -->
      <el-form-item label="密码">
        <el-input v-model="userInfo.password" type="password" class="small-input"></el-input>
      </el-form-item>
      <!-- 动态显示病情信息输入框 -->
      <el-form-item label="病情信息" v-if="userInfo.role === '普通用户'">
        <el-input type="textarea" v-model="userInfo.disease" class="small-input"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="submitForm">保存修改</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
export default {
  name: 'PersonalInfoManagement',
  data() {
    return {
      userInfo: {
        // 设置默认头像
        avatar: require('@/assets/images/Avatar2.png'), 
        username: '默认用户名',
        age: 20,
        gender: '男',
        phone: '13800138000',
        email: 'default@example.com',
        role: '普通用户',
        disease: '暂无病情信息',
        // 新增默认密码
        password: '123456' 
      }
    };
  },
  methods: {
    handleAvatarSuccess(res, file) {
      this.userInfo.avatar = URL.createObjectURL(file.raw);
    },
    beforeAvatarUpload(file) {
      const isJPG = file.type === 'image/jpeg';
      const isLt2M = file.size / 1024 / 1024 < 2;

      if (!isJPG) {
        this.$message.error('头像图片只能是 JPG 格式!');
      }
      if (!isLt2M) {
        this.$message.error('头像图片大小不能超过 2MB!');
      }
      return isJPG && isLt2M;
    },
    submitForm() {
      // 这里可以添加保存数据到后端的逻辑
      this.$message.success('个人信息保存成功');
    }
  }
};
</script>

<style scoped>
.personal-info-container {
  padding: 20px;
  display: flex;
  justify-content: center;
  align-items: center;
  min-height: 100vh; /* 确保容器至少占满视口高度 */
}

/* 让上传组件居中 */
.avatar-uploader {
  display: flex;
  justify-content: center;
}

.avatar-uploader .avatar {
  width: 178px;
  height: 178px;
  display: block;
}

.avatar-uploader .el-upload {
  border: 1px dashed var(--el-border-color);
  border-radius: 50%; /* 让上传组件变为圆形 */
  cursor: pointer;
  position: relative;
  overflow: hidden;
}

.avatar-uploader .el-upload:hover {
  border-color: var(--el-color-primary);
}

.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  text-align: center;
  line-height: 178px; /* 让图标垂直居中 */
}

/* 让头像显示为圆形 */
.circular {
  border-radius: 50%;
}

/* 统一缩小输入框宽度 */
.small-input {
  width: 300px; 
}

/* 让整个表单居中 */
.centered-form {
  display: flex;
  flex-direction: column;
  align-items: center;
}

/* 为表单添加底色和阴影 */
.styled-form {
  background-color: #ffffff; /* 表单底色 */
  padding: 40px;
  border-radius: 10px;
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.1); /* 添加阴影 */
}
</style>